module.exports = function($scope, $modal, $resource, getDate, toaster){

    //加载活动列表-初始化执行一次
    $scope.loadActivity = function() {
        $resource('/api/as/ggc/activity/getUseingList', {}, {}).save({}, function (res) {
            if (res.errcode === 0){
                $scope.activities = res.data;
                $scope.activities.unshift({'title': '全部公益游', 'id': undefined});
            } else {
                toaster.error({title: '', body: '查询活动列表失败, 请刷新页面'});
            }
        });
    }

    //查询机构列表
    $scope.search = function(pageNo){
        var para = {
			'pageNo': pageNo,
			'pageSize': $scope.itemsPerPage,
            'company_name': $scope.fromData.company_name,
            'activity_id': $scope.fromData.activity_id
		}
        $resource('/api/as/ggc/volaccount/getAccountList', {}, {}).save(para, function (res) {
            if (res.errcode === 0){
                $scope.list = res.data.results;
                $scope.totalItems = res.data.totalRecord;
                $scope.currentPage = pageNo;
                $scope.fromData.loading = false;
            } else {
                toaster.error({title: '', body: '查询活动列表失败, 请刷新页面'});
            }
        });
    }

    init();
    //初始化页面信息
    function init(){
        $scope.fromData = {
            'loading': true,
            'activities': {}
        };
        $scope.currentPage = 1;			//当前页码
		$scope.itemsPerPage = 10;		//每页显示几条
        $scope.loadActivity();
        $scope.search(1);
    }

    //打开日历空间,选择时间
    $scope.dateOpen = function($event, item){
        $event.preventDefault();
        $event.stopPropagation();
        item.opened = true;
    }

    $scope.add = function(info) {
        var modalInstance = $modal.open({
			template: require('../views/addSaleAllot.html'),
			controller: 'addSaleAllot',
			size: 'lg',
			resolve: {
				activities: function () {
					return $scope.activities;
				},
				getDate: function () {
					return getDate;
				},
				userinfo: function () {
					return info;
				}
			}
		});
		//关闭模态框刷新页面
		modalInstance.result.then(function(result) {
            $scope.search(1);
		});
    }

    $scope.update = function(info){
        info.update = true;
    }

    $scope.save = function(info) {
        if(info.tour_stock <= 0){
            toaster.warning({title: '', body: '修改名额错误'});
            return;
        }
        $resource('/api/ac/ggc/voluntourismAccountService/updateAccountNum', {}, {}).save(info, function(res){
            if (res.errcode === 0){
                toaster.success({title: '', body: '修改成功'});
                info.update = false;
            } else {
                toaster.error({title: '', body: res.errmsg});
            }
        });
    }

    $scope.delete = function(item) {
        if(confirm('确认删除本条公益游活动吗？')){
            $resource('/api/ac/ggc/voluntourismAccountService/deleteAccount', {}, {}).save({'id': item.id}, function (res) {
                if (res.errcode === 0){
                    toaster.success({title: '', body: '删除成功'});
                    $scope.search(1);
                } else {
                    toaster.error({title: '', body: '删除失败'});
                }
            });
        }
    }

};